package com.zrrd.learn.chapter08.thread;

/**
 * 同步锁
 *
 * @author asus
 * @version 1.0
 * @date 2023/8/3 8:58
 */
public class Synchronized {

    private static int a = 0;
    private static int a1 = 0;

    private static final Object obj = new Object();


    //    private synchronized   static  void add(){
//        a++;
    private static void add() {
        synchronized (obj) {
            a++;
        }
    }

    //
//        public static void main(String[] args) {
//            Thread t1 = new Thread(()->{

    public static void main(String[] args) throws Exception {

        Thread t1 = new Thread(() -> {

            for (int i = 0; i < 10000; i++) {
//                            add();
                add();
            }
        });


//                    Thread t2 = new Thread(()->{
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                add();
            }
        });

        //计时 时间 値
        long s1 = System.currentTimeMillis();
        t1.start();
        t2.start();

//        并发数  : 高并发

        // main结束
        //捕获处理 try chatch
//                    try{
        try {
            t1.join();
            t2.join();
//                        }catch (Exception e){
        } catch (InterruptedException e) {
            System.out.println("出错了");
            e.printStackTrace();
        }

        System.out.println(a);
        long s2 = System.currentTimeMillis();
        //0 -20000

//         System.out.println("用时==>" + (s2-s1));

        System.out.println("用时==>" + (s2 - s1));
    }

}